/* Exercise 5.32
   ejer5_32.c */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Function prototipes */
int getNumber( void );

/* function main begins program execution */
int main( void )
{
    int number, input;
    char resp;

    srand( time( NULL ));
    do
    {
        number = getNumber();

        printf( "I have a number between 1 and 1000\nCan you guess the number?\nPlease type your first guess " );
        scanf( "%d", &input );

        while ( input != number )
        {
            if ( input < number )
                printf( "Too low. Try again. ");
            else
                printf( "Too high. Try again. ");
            scanf( "%d", &input );
        }

        printf ("Excellent! You guessed the number!\nWould you like to play again (y or n)? ");
        resp = getchar();

        while ( resp == '\n' || resp == '\t' || resp == ' ' )
            resp = getchar();
    } while ( resp != 'n' && resp != 'N' );

	return 0; /* indicate that program ended successfully */
} /* end function main */

/* This function returns a random number between 1 and 1000 */
int getNumber( void )
{
    return rand() % 1000 + 1;
}
